
#include "config.h"#include <stdio.h>
#include <time.h>
#include <inttypes.h>#include <amanita/aMath.h>
#include <amanita/aVector.h>
//#include <amanita/sqlite/aSQLite.h>
#include <amanita/net/aHttp.h>
#include "Main.h"
#include "EditPage.h"
#include "astro/Astrology.h"



void EditPage::save_callback(GtkWidget* widget, gpointer data) {
	EditPage &page = *(EditPage *)data;
	page.saveText();
}

void EditPage::find_callback(GtkWidget* widget, gpointer data) {
//	EditPage &page = *(EditPage *)data;
}

void EditPage::findreplace_callback(GtkWidget* widget, gpointer data) {
//	EditPage &page = *(EditPage *)data;
}

void EditPage::undo_callback(GtkWidget* widget, gpointer data) {
//	EditPage &page = *(EditPage *)data;
}

void EditPage::redo_callback(GtkWidget* widget, gpointer data) {
//	EditPage &page = *(EditPage *)data;
}

void EditPage::help_callback(GtkWidget* widget, gpointer data) {
//	EditPage &page = *(EditPage *)data;
}


EditPage::EditPage(const char *uri) : Page(),symbol(0),id(0) {
	if(uri && !*uri && !strncmp(uri,"symbol://",9)) {
		char p[strlen(uri)-8],*p1,*p2;
		strcpy(p,&uri[6]);
		p1 = strchr(p,'#');
		if(p1) *p1++ = '\0';
		p2 = strchr(p1? p1 : p,'?');
		if(p2) *p2++ = '\0';

		id = (uint64_t)(p1? atoll(p1) : 0);
		symbol = (symbol_t)atoll(p);
	}
	init(symbol,id);
}

EditPage::EditPage(symbol_t s,uint64_t i) : Page(),symbol(0),id(0) {
	init(s,i);
}

void EditPage::init(symbol_t s,uint64_t i) {
app.printf("EditPage::EditPage(symbol=%" PRIu64 ",id=%" PRIu64 ")",s,i);

	GtkWidget *page = gtk_vbox_new(FALSE,0);

	GtkWidget* toolbar = gtk_toolbar_new();
	gtk_orientable_set_orientation(GTK_ORIENTABLE(toolbar),GTK_ORIENTATION_HORIZONTAL);
	gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),GTK_TOOLBAR_BOTH_HORIZ);
	{
		GtkToolItem *item;
		item = gtk_tool_button_new_from_stock(GTK_STOCK_SAVE);
		g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(save_callback),this);
		gtk_toolbar_insert(GTK_TOOLBAR(toolbar),item,-1);

		item = gtk_tool_button_new_from_stock(GTK_STOCK_FIND);
		g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(find_callback),this);
		gtk_toolbar_insert(GTK_TOOLBAR(toolbar),item,-1);

		item = gtk_tool_button_new_from_stock(GTK_STOCK_FIND_AND_REPLACE);
		g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(findreplace_callback),this);
		gtk_toolbar_insert(GTK_TOOLBAR(toolbar),item,-1);

		item = gtk_tool_button_new_from_stock(GTK_STOCK_UNDO);
		g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(undo_callback),this);
		gtk_toolbar_insert(GTK_TOOLBAR(toolbar),item,-1);

		item = gtk_tool_button_new_from_stock(GTK_STOCK_REDO);
		g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(redo_callback),this);
		gtk_toolbar_insert(GTK_TOOLBAR(toolbar),item,-1);

		item = gtk_tool_button_new_from_stock(GTK_STOCK_HELP);
		g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(help_callback),this);
		gtk_toolbar_insert(GTK_TOOLBAR(toolbar),item,-1);
	}
	gtk_box_pack_start(GTK_BOX(page),toolbar,FALSE,FALSE,0);

	GtkWidget *scroll = gtk_scrolled_window_new(NULL,NULL);
//	gtk_widget_set_size_request(scroll,330,-1);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),GTK_POLICY_AUTOMATIC,GTK_POLICY_ALWAYS);
//	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),GTK_SHADOW_IN);
	{
		//gtk.font = gdk_font_load("-misc-fixed-bold-r-normal--13-*-*-*-*-*-*");
		gtk.editor = gtk_text_view_new();
		gtk.buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(gtk.editor));
//		gtk_text_buffer_get_iter_at_offset(gtk.buffer,&gtk.iter,0);
		gtk_container_add(GTK_CONTAINER(scroll),gtk.editor);
	}
	gtk_box_pack_end(GTK_BOX(page),scroll,TRUE,TRUE,0);

	setText(s,i);

	Page::open("edit.text",page);
}

EditPage::~EditPage() {
app.printf("EditPage::EditPage()");
}

void EditPage::setText(symbol_t s,uint64_t i) {
	symbol = s;
	id = i;

	if(id) {
		aHttp http(app);
		setText(http.getf(WEB_HOST,WEB_PATH "text.php?id=%" PRIu64,id));
	} else {
		setText(aString::blank);
	}
}

void EditPage::setText(const char *txt) {
	gtk_text_buffer_set_text(gtk.buffer,txt,-1);
}

void EditPage::saveText() {
	GtkTextIter start,end;
	gtk_text_buffer_get_bounds(gtk.buffer,&start,&end);
	char *txt = gtk_text_buffer_get_text(gtk.buffer,&start,&end,FALSE);
//	aString str(txt);
//	free(txt);
//app.printf("EditPage::saveText(text=%s)",str.toCharArray());
//	str.replace("'","''");
//app.printf("EditPage::saveText(text=%s)",str.toCharArray());
	aHttp http(app);
//	http.setFormValue("cmd","save");
//	http.setFormValue("uid","1");
//	http.setFormValue("pwd","1");
//	http.setFormValue("lang","sv");
//	http.setFormValue("symbol","%" PRIu64,symbol);
//	http.setFormValue("id","%" PRIu64,id);
//	http.setFormValue("text","%s",txt);
//	http.post(WEB_HOST,WEB_PATH "text.php");
	http.setFormValue("text","%s",txt);
	http.postf(WEB_HOST,WEB_PATH "text.php?cmd=save&lang=%s&k=%s&symbol=%" PRIu64 "&id=%" PRIu64,"sv",app.local_key,symbol,id);
	free(txt);
/*	if(status!=1) return;
	aSQLite sql("files/text.sv.dat");
	int i,len = SendMessage(edit,WM_GETTEXTLENGTH,0,0),n;
	char str[len*2+1];
	SendMessage(edit,WM_GETTEXT,(WPARAM)len+1,(LPARAM)str);
	for(i=0,n=0; i<len; i++) if(str[i]=='\'') n++;
	char buf[256+len+n+1];
	if(n) for(i=len; n && i>=0; i--) {
		str[i+n] = str[i];
		if(str[i]=='\'') str[i+ --n] = '\'';
	}
	if(id) sprintf(buf,"update 'text' set 'text'='%s' where id=%I64d;",str,id);
	else sprintf(buf,"insert into 'text' values(null,%d,%d,%I64d,'%s');",1,0,doc,str);
app.printf("TextMDI::saveText(id=%I64d,doc=%I64d,str='%s',buf='%s')",id,doc,str,buf);
	sql.query(buf);

	setDocument(doc);*/
}

//void TextPage::writeTitle(aString &title,Symbol &c,symbol_t *refs) {
//	switch((int)c.system) {
//		case OCCULT_ASTRO:Astrology::getCodeHeader(title,c,&refs[1]);break;
//		case OCCULT_QABALAH:
//		case OCCULT_TAROT:
//		case OCCULT_ENOCH:
//		case OCCULT_ALCHEMY:
//		case OCCULT_WICCA:title.append(app.occult.system(c.system));break;
//	}
//}


